package io.daocloud.prometheustestdemo.controller;

import io.daocloud.prometheustestdemo.service.RateLimitService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Hello REST API 控制器
 */
@RestController
@Slf4j
public class HelloController {
    
    private static final Logger log = LoggerFactory.getLogger(HelloController.class);
    
    @Autowired
    private RateLimitService rateLimitService;
    
    /**
     * Hello接口
     * @return 固定JSON数据 {"msg": "hello"}
     */
    @GetMapping("/hello")
    public Map<String, String> hello() {
        log.info("Hello endpoint accessed");
        Map<String, String> response = new HashMap<>();
        response.put("msg", "hello");
        return response;
    }
    
    /**
     * 诊断接口 - 测试Redis连接和限流服务状态
     * @return 诊断信息
     */
    @GetMapping("/diagnostic")
    public Map<String, Object> diagnostic() {
        log.info("Diagnostic endpoint accessed");
        Map<String, Object> response = new HashMap<>();
        
        try {
            // 测试限流服务
            boolean rateLimitResult = rateLimitService.isAllowed("diagnostic_test_key");
            response.put("rateLimitService", "OK");
            response.put("rateLimitTestResult", rateLimitResult);
            response.put("status", "SUCCESS");
        } catch (Exception e) {
            log.error("Diagnostic test failed", e);
            response.put("rateLimitService", "FAILED");
            response.put("error", e.getMessage());
            response.put("status", "ERROR");
        }
        
        response.put("timestamp", System.currentTimeMillis());
        return response;
    }
} 